<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/17 16:40
 * Desc:
 */
class Service_Api_Question extends Contract_Api_Question
{

    public function question_list($params): array
    {
        $cfg_question_is_show = Model_Sysconfig::get_configs(0, 'cfg_question_is_show', true);
        if ($cfg_question_is_show == 0) {
            return array('list' => [], 'total' => 0);
        }

        $type          = Arr::get($params, 'type', 1);   //1：会员的问答，2非会员问答
        $pagesize      = Arr::get($params, 'pagesize', 10);
        $page          = Arr::get($params, 'page', 1);
        $ques_type     = Arr::get($params, 'ques_type', 0); // 问题类型，0为产品相关问答，1为一般问答
        $article_id    = Arr::get($params, 'article_id', ''); // 文章所在表的id
        $type_id       = Arr::get($params, 'type_id', ''); // 产品id
        $mid           = Arr::get($params, 'mid', '');
        $keyword       = Arr::get($params, 'keyword', '');
        $search_params = array(
            'page' => $page,
            'pagesize' => $pagesize,
            'keyword' => $keyword,
            'ques_type' => $ques_type,
            'article_id' => $article_id,
            'type_id' => $type_id,
        );

        // 会员的问答
        if ($type == 1) {
            $search_params['mid']       = $mid;
            $search_params['ques_type'] = '';
        }

        $data = Model_Api_V2_Question::search($search_params);

        $filter_keys = 'id,title,content,product_name,addtime,replytime,replycontent';
        // 预处理数据
        foreach ($data['list'] as &$v) {
            $v['addtime']      = date("Y-m-d H:i", $v['addtime']);
            $v['replytime']    = $v['replytime'] ? date("Y-m-d H:i", $v['replytime']) : '';
            $v['replycontent'] = $v['replycontent'] ? $this->content_add_domain($v['replycontent']) : '';
            $v['title']        = !empty($v['title']) ? $v['title'] : Common::cutstr_html($v['content'], 20);
            $product_name      = '';
            if ($v['typeid'] && $v['productid']) {
                $product_info = Model_Api_V2_Question::get_product_info($v['typeid'], $v['productid']);
                $product_name = $product_info['title'];
            }
            $v['product_name'] = $product_name;

            $v = $this->filter_data($filter_keys, $v);
        }

        return $data;
    }

    public function add_question($params): array
    {
        /**
         * @param $captcha_key
         * @param $code
         * @param $type_id
         * @param $article_id
         * @param $mobile
         * @param $is_anonymous
         * @param $content
         * @param $mid
         * @param $ques_type
         * @param $title
         */
        extract($params);

        if (!Model_Api_V2_Common::verify_image_code($captcha_key, $code)) {
            throw new Exception(__("checkcode_error"));
        }

        if($ques_type == 0) {
            $product = Model_Api_V2_Question::get_product_info($type_id, $article_id);
            if(empty($product)){
               throw new Exception(__("product_not_exist"));
            }
        }

        $arr              = array();
        $arr['typeid']    = !empty($type_id) && $ques_type == 0  ? $type_id : '';
        $arr['productid'] = !empty($article_id) && $ques_type == 0 ? $article_id : '';
        $arr['title']     = !empty($title) ? $title : '';
        $arr['content']   = $content;
        $arr['phone']     = $mobile;
        $arr['questype']  = $ques_type;
        $arr['addtime']   = time();

        // 是否匿名
        $member = Model_Member::get_member_byid($mid);
        if ($isanonymous == 'false') {
            $arr['memberid'] = $member['mid'];
            $arr['nickname'] = $member['nickname'];
        } else {
            $arr['memberid'] = $member['mid'];
            $arr['nickname'] = '匿名';
        }

        $flag = DB::insert('question', array_keys($arr))->values(array_values($arr))->execute();
        if ($flag) {
            $jifen = Model_Jifen::reward_jifen('sys_write_wenda', $arr['memberid']);
            if (!empty($jifen)) {
                St_Product::add_jifen_log($arr['memberid'], '发布问答送积分' . $jifen, $jifen, 2);
            }
        }

        // 验证成功后清除图片缓存
        Model_Api_V2_Common::cache('set', $captcha_key, null);

        return array(
            'status' => true,
        );
    }
}